import {reqUserList,reqAddUser,reqDeleteUser,reqUpdateUser} from '@/api'
import { Message } from 'element-ui'
const actions = {
  async getUserList({commit},data){
    const res = await reqUserList(data)
    if(res.code == 200){
      commit("GETUSERLIST",res.list)
      return 'ok'
    }
  },
  async addUser({commit},data){
    const res = await reqAddUser(data)
    if(res.code == 200){
      Message.success('添加成功')
      commit('ADDUSER')
      return 'ok'
    }
  
  },
  async updateUser({commit},user){
    const res = await reqUpdateUser(user)
    if(res.code == 200){
      commit('UPDATEUSER')
      Message.success('编辑成功')
      return 'ok'
    }
  },
  async deleteUser(_,userId){
    console.log(userId,'id');
    const res =await reqDeleteUser({id:userId})
    if(res.code == 200){
      Message.success('删除成功')
      return 'ok'
    }else{
      Message.error("删除失败")
    }
  }
}
const mutations = {
  ADDUSER(state){
    state.formData.dialogVisible = true
    state.modelType = 1
  },
  CANCELDIALOG(state){
    state.formData.dialogVisible = false
  },
  GETUSERLIST(state,data){
    state.formData.userList = data
  },
  EDITUSER(state){
    state.formData.dialogVisible = true
    state.modelType = 0
  },
  UPDATEUSER(state){
    state.formData.dialogVisible = false
  }
}

const state = {
  modelType:1,
  // 添加，编辑用户组件的显示与隐藏
  formData:{
    dialogVisible:false,
    userList:[],
  }
}

const getters = {
  userList(state){
    return state.formData.userList
  }
}
export default {
  namespaced:true,
  state,
  actions,
  mutations,
  getters
}